// src/services/scholarsDashboardService/fetchScholarBatchUpdate.ts

import { BASE_URL } from '@/services/apiConfig';

interface ITEDashboardManualRecord {
  ite_id: number;
  indigenous_student: string | null;
  placement_performance: string | null;
  grade_updated: string | null;
  grade_updated_date: string | null;
  unsw_attendee_presentation: string | null;
  mpc_submitted_date: string | null;
  mpc_submitted_on_time: string | null;
  mpc_call_date: string | null;
  mpc_call_on_time: string | null;
  it_change_approved: string | null;
  // Scholar information fields
  first_name: string | null;
  last_name: string | null;
  preferred_name: string | null;
  gender: string | null;
}

interface ITEDashboardManualParams {
  skip?: number;
  limit?: number;
}

interface ITEDashboardManualResponse {
  total: number;
  records: ITEDashboardManualRecord[];
}

export const fetchScholarBatchUpdate = async (
  params: ITEDashboardManualParams = {}
): Promise<ITEDashboardManualResponse> => {
  const query = new URLSearchParams();
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined && value !== null) {
      query.append(key, String(value));
    }
  });

  const token = localStorage.getItem('token');
  if (!token) throw new Error('No token found');

  const res = await fetch(`${BASE_URL}/scholars/ite-dashboard-manual?${query.toString()}`, {
    method: 'GET',
    headers: {
      'Authorization': `Bearer ${token}`,
    },
  });

  if (!res.ok) {
    const err = await res.text();
    throw new Error(`Fetch failed: ${res.status} - ${err}`);
  }

  const data: ITEDashboardManualResponse = await res.json();
  return data;
};
